//
// Created by EDZ on 2021/11/1.
//

#include <iostream>
#include <mysql++.h>
#include "DbQuery.h"

namespace MyDB {

    using namespace mysqlpp;
    using namespace std;

    DbQuery::~DbQuery() {
        cout << "~DbQuery() destructor" << endl;
    }

    DbQuery::DbQuery() {
        cout << "DbQuery() constructor" << endl;
    }

    int DbQuery::Exec() {
        try {
            Connection conn(false);
            conn.connect("", "localhost", "root", "123456");
            Query query = conn.query("show create database cppdemo");
            query.exec("CREATE DATABASE IF NOT EXISTS cppdemo DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;");
            if (StoreQueryResult res = query.store()) {
                cout << "We have:" << endl;
                StoreQueryResult::const_iterator  it;
                for (it=res.begin(); it!=res.end(); ++it) {
                    Row row = *it;
                    cout << "\t" << row[0] << endl;
                }
            }

        } catch (BadQuery &er) {
            cout << "Error:" << er.what() << endl;
            return -1;
        } catch (const BadConversion &er) {
            cout << "Conversion error: " << er.what() << endl <<
                 "\tretrieved data size: " << er.retrieved <<
                 ", actual size: " << er.actual_size << endl;
            return -1;
        } catch (const Exception &er) {
            // Catch-all for any other MySQL++ exceptions
            cout << "Error: " << er.what() << endl;
            return -1;
        }

        cout << "no problem" << endl;

        return 0;
    }
}